/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package FrameSekretariat;

import Model.PengendaliDataSekretariat;
import Model.PengendaliUser;
import Model.Surat;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

/**
 *
 * @author User
 */
public class SekreTambahSurat extends JFrame {

    private JLabel nosrt, perihal, asal, penerima, idPenerima, petugas, tanggal, tempat;
    private JTextField textnosrt, textperihal, textasal, textpenerima, textId, textPetugas, textTanggal;
    private JButton buttonOk, buttonCancel;
    private JRadioButton tonLoker, tonRuangan, tonLemari;

    public SekreTambahSurat() {
        this.setTitle("Form Surat");
        this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        this.setBounds(400, 200, 370, 450);
        this.setLayout(null);

        nosrt = new JLabel("Nomor ");
        nosrt.setBounds(20, 25, 80, 25);
        this.add(nosrt);

        perihal = new JLabel("Perihal ");
        perihal.setBounds(20, 65, 80, 25);
        this.add(perihal);

        asal = new JLabel("Pengirim");
        asal.setBounds(20, 105, 80, 35);
        this.add(asal);

        penerima = new JLabel("Nama Penerima");
        penerima.setBounds(20, 150, 100, 25);
        this.add(penerima);

        idPenerima = new JLabel("ID Penerima");
        idPenerima.setBounds(20, 195, 90, 25);
        this.add(idPenerima);

        petugas = new JLabel("Petugas Terima");
        petugas.setBounds(20, 235, 105, 25);
        this.add(petugas);

        tanggal = new JLabel("Tanggal Terima");
        tanggal.setBounds(20, 275, 90, 25);
        this.add(tanggal);

        tempat = new JLabel("Tempat Simpan");
        tempat.setBounds(20, 315, 105, 25);
        this.add(tempat);

        textnosrt = new JTextField();
        textnosrt.setBounds(130, 25, 205, 25);
        this.add(textnosrt);

        textperihal = new JTextField();
        textperihal.setBounds(130, 65, 205, 25);
        this.add(textperihal);

        textasal = new JTextField();
        textasal.setBounds(130, 105, 205, 25);
        this.add(textasal);

        textpenerima = new JTextField();
        textpenerima.setBounds(130, 150, 205, 25);
        this.add(textpenerima);

        textId = new JTextField();
        textId.setBounds(130, 195, 205, 25);
        this.add(textId);

        textPetugas = new JTextField();
        textPetugas.setBounds(130, 235, 205, 25);
        this.add(textPetugas);

        textTanggal = new JTextField();
        textTanggal.setBounds(130, 275, 205, 25);
        this.add(textTanggal);

        tonLoker = new JRadioButton("Loker");
        tonLoker.setBounds(125, 315, 60, 25);
        this.add(tonLoker);

        tonLemari = new JRadioButton("Lemari");
        tonLemari.setBounds(190, 315, 70, 25);
        this.add(tonLemari);

        tonRuangan = new JRadioButton("Ruangan");
        tonRuangan.setBounds(260, 315, 80, 25);
        this.add(tonRuangan);

        ButtonGroup bg = new ButtonGroup();
        bg.add(tonLoker);
        bg.add(tonLemari);
        bg.add(tonRuangan);

        buttonOk = new JButton("OK");
        buttonOk.setBounds(80, 360, 80, 30);
        this.add(buttonOk);
        buttonOk.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (textnosrt.getText().equals("") || textperihal.getText().equals("") || textasal.getText().equals("")
                        || textpenerima.getText().equals("") || textId.getText().equals("") || textPetugas.getText().equals("")
                        || textTanggal.getText().equals("")) {
                    JOptionPane.showMessageDialog(null, "Lengkapi semua data terlebih dahulu");
                } else {
                    Surat surat = new Surat();
                    try {
                        surat.setNomor_surat(textnosrt.getText());
                        surat.setPerihal(textperihal.getText());
                        surat.setAsal_surat(textasal.getText());
                        surat.setPenerima_surat(textpenerima.getText());
                        surat.setId_penerima(textId.getText());
                        if (tonLoker.isSelected()) {
                            surat.setPilihLetak(1);
                        } else if (tonLemari.isSelected()) {
                            surat.setPilihLetak(2);
                        } else if (tonRuangan.isSelected()) {
                            surat.setPilihLetak(3);
                        }
                        surat.setPetugas_penerima(textPetugas.getText());
                        surat.setTanggal_terima(textTanggal.getText());
                        PengendaliDataSekretariat.tambahSurat(surat);
                        try {
                            PengendaliDataSekretariat.tambahTabelSurat(surat);
                            JOptionPane.showMessageDialog(null, "Data Tersimpan");
                            textId.setText(null);
                            textPetugas.setText(null);
                            textTanggal.setText(null);
                            textasal.setText(null);
                            textnosrt.setText(null);
                            textpenerima.setText(null);
                            textperihal.setText(null);
                        } catch (SQLException ex) {
                            JOptionPane.showMessageDialog(null, "Data yang anda masukan sudah ada");
                        }
                    } catch (Exception ex) {
                        JOptionPane.showMessageDialog(null, ex.getMessage());
                    }
                }
            }
        });

        buttonCancel = new JButton("CANCEL");
        buttonCancel.setBounds(195, 360, 80, 30);
        this.add(buttonCancel);
        buttonCancel.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
            }
        });
    }
}
